package com.eco.fanliapp.job.messagepushjob;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eco.fanliapp.common.response.ServerResponse;
import com.eco.fanliapp.entity.MessagePush;
import com.eco.fanliapp.service.IMessagePushService;
import com.eco.fanliapp.utils.DateFormatUtils;
import org.quartz.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.Serializable;
import java.util.List;

/**
 * web消息推送定时任务
 */
@DisallowConcurrentExecution
@PersistJobDataAfterExecution
public class MessagePushTask implements Job, Serializable {


    private static final Logger log = LoggerFactory.getLogger(MessagePushTask.class);

    @Autowired
    private IMessagePushService iMessagePushService;

    public MessagePushTask() {
    }

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {

        //取得job详情
        JobDetail jobDetail = context.getJobDetail();

        JobDataMap dataMap =  jobDetail.getJobDataMap();

        int executeCount = 0;
        if (dataMap.containsKey("runCount")) {
            executeCount = dataMap.getInt("runCount");
        }

        // 增量计数并将其存储回 JobDataMap，这样可以适当保持工作状态
        executeCount++;
        dataMap.put("runCount", executeCount);

        long startTime = System.currentTimeMillis();
        log.info("***************************************开始执行web消息推送定时任务-MessagePushTask：executeCount=" + executeCount + "--startTime=" + startTime);

        try {
            String dateStr = DateFormatUtils.stampToDate2(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss");
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.le("push_time",dateStr);
            queryWrapper.eq("status",0);
            List<MessagePush> messagePushList = iMessagePushService.list(queryWrapper);
            for (MessagePush messagePush : messagePushList) {
                log.info("推送数据{}", JSONObject.toJSONString(messagePush));
                ServerResponse serverResponse = iMessagePushService.insert(messagePush, 3);
                log.info("推送数据结果{}", JSONObject.toJSONString(serverResponse));
                if(serverResponse.getStatus() == 200){
                    MessagePush newMessagePush = new MessagePush();
                    newMessagePush.setId(messagePush.getId());
                    newMessagePush.setStatus(1);
                    boolean b = iMessagePushService.updateById(newMessagePush);
                    log.info("修改推送状态是否成功{}",b);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.info("执行web消息推送定时任务错误");
        }

        long endTime = System.currentTimeMillis();
        log.info("***************************************结束执行web消息推送定时任务-MessagePushTask：executeCount=" + executeCount + "--endTime=" + endTime + "--耗时=" + (endTime - startTime));

    }
}
